package com.service.xbq;

import com.dao.BaseDao;
import com.entity.CatPost;
import com.entity.User;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.lang.reflect.Field;
import java.sql.SQLException;

public class RelInformationService extends BaseDao {
    public boolean addUser(CatPost catPost) {
        /*String sql = "INSERT INTO `rz1217`.`user`(`username`, `password`, " +
                "`name`, `sex`, `age`, `birthday`, `phone`, " +
                "`height`, `pic`) " +
                "VALUES (?,?,?,?,?,?,?,?,?)";
        Object[] params = getObjArr(user);*/
        try {
            System.out.println(catPost);

            return insert(catPost) > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public int insertInformation(Object obj) throws Exception {
        //1.获取对象的反射
        Class clas = obj.getClass();
        //获取类名
        String tableName = clas.getSimpleName();
        //2.拼接sql
        String sql = "INSERT INTO "+tableName;
        //参数部分
        String sqlParams = "(";
        //占位符部分
        String sqlZ = "(";
        //循环所有的字段
        Field[] fields = clas.getDeclaredFields();
        //参数
        Object[] params = new Object[fields.length];
        int i = 0;
        for (Field field : fields) {
            String name = field.getName();
            sqlParams += name+",";
            sqlZ += "?,";
            //调用get方法
            //获取方法

            String getMethodName = "get" + name.substring(0,1).toUpperCase() + name.substring(1);
            //如果id有值，则直接复制，否则赋值为空值，让数据库自增。
            if (name.equals("id")) {
                Object idValue = clas.getDeclaredMethod(getMethodName).invoke(obj);
                if (idValue.equals(0))
                    params[i] = null;
                else
                    params[i] = idValue;
                i++;
                continue;
            }
            params[i] = clas.getDeclaredMethod(getMethodName).invoke(obj);
            i++;
        }
        //去掉最后一个逗号
        sqlParams = sqlParams.substring(0, sqlParams.length()-1);

        sqlZ = sqlZ.substring(0, sqlZ.length()-1);
        sqlParams += ")";
        sqlZ += ")";

        sql = sql + sqlParams + " values " + sqlZ;
        System.out.println(sql);
        return update(sql, params);
    }

    public void cut_number(String username){
      int count =   queryNumber(username);
      //  System.out.println(count);
        count =count-1;
        String sql2 = "UPDATE `rz1217db_2`.`user` SET `number` = ? WHERE `username` = ?";
        update(sql2,count,username);
    }


    public int count2(String sql,String catpost) {
        try {
            Integer l = runner.query(sql, new ScalarHandler<>(),catpost);
            return l;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    public static void main(String[] args) {
        new RelInformationService().cut_number("xbq");
    }

    //查询用户剩余发帖量
    public  int queryNumber(String username){
        String sql = "select number from user where username = ?";
        int count = count2(sql,username);
        return count;
    }

    //查询用户已发帖量


}
